package com.six666.medicaldevicetestingsystem.controller;

import com.six666.medicaldevicetestingsystem.dto.Equipment_Order_DTO;
import com.six666.medicaldevicetestingsystem.entity.BaseResponse;
import com.six666.medicaldevicetestingsystem.service.AccountOrder_Service;
import com.six666.medicaldevicetestingsystem.service.GoodsList_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
public class Order_Controller {
    @Autowired
    AccountOrder_Service accountorder_service;
    @Autowired
    GoodsList_Service goodslist_service;
    @PostMapping("/del_order")
    public BaseResponse<Boolean> del_order(@RequestParam Map<String, String> params) {

        if(params.get("order_id")==null){
            return new BaseResponse<>(101,null,"参数错误","order_id为空");
        }
        int order_id = Integer.parseInt(params.get("order_id"));
        boolean res=accountorder_service.delete_order(order_id);
        return new BaseResponse<>(res);
    }
    @PostMapping("/change_order_num")
    public BaseResponse<Boolean> change_order_num(@RequestParam Map<String, String> params) {
        int order_id;
        int order_num;
        int goods_id;
        try {
            order_id = Integer.parseInt(params.get("order_id"));
            order_num = Integer.parseInt(params.get("num"));
            goods_id = Integer.parseInt(params.get("goods_id"));
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResponse<>(101, null, "参数错误", "参数为空或非整形");
        }
        boolean res = goodslist_service.change_goods_amount(order_id, goods_id, order_num);
        return new BaseResponse<>(res);
    }
    @PostMapping("/get_personal_equip_order")
    public BaseResponse<List<Equipment_Order_DTO>> get_personal_equip_order(@RequestParam Map<String, String> params) {
        int uid;
        String type=params.get("type");
        if(type==null|| type.isEmpty()){
            type="OngoingAndFinish";
        }
        else if(!(type.equals("OngoingAndFinish") || type.equals("Finish") || type.equals("Ongoing") || type.equals("Preformed"))){
            return new BaseResponse<>(101, null, "参数错误", "参数为空或非整形");

        }
        try {
            uid = Integer.parseInt(params.get("uid"));
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResponse<>(103, null, "参数错误", "type取值错误");
        }
        List<Equipment_Order_DTO> res = accountorder_service.get_personal_equip_order(uid,type);
        return new BaseResponse<>(res);
    }
    @GetMapping("/get_all_equip_order")
    public BaseResponse<List<Equipment_Order_DTO>> get_all_equip_order() {
        List<Equipment_Order_DTO> res = accountorder_service.get_all_equip_order();
        return new BaseResponse<>(res);
    }
    @PostMapping("/change_order_state")
    public BaseResponse<Boolean> change_order_state(@RequestParam Map<String, String> params) {
        int order_id;
        try {
            order_id = Integer.parseInt(params.get("order_id"));
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResponse<>(101, null, "参数错误", "参数为空或非整形");
        }
        return new BaseResponse<>(accountorder_service.change_state(order_id));
    }
    @PostMapping("/change_preformed_order_state")
    public BaseResponse<Boolean> change_preformed_order_state(@RequestParam Map<String, String> params) {
        int order_id;
        try {
            order_id = Integer.parseInt(params.get("order_id"));
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResponse<>(101, null, "参数错误", "参数为空或非整形");
        }
        return new BaseResponse<>(accountorder_service.change_preformed_state(order_id));
    }
    @PostMapping("/delete_one_good")
    public BaseResponse<Boolean> delete_one_good(@RequestParam Map<String, String> params) {
        int good_id;
        int order_id;
        try {
            order_id = Integer.parseInt(params.get("order_id"));
            good_id = Integer.parseInt(params.get("good_id"));
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResponse<>(101, null, "参数错误", "参数为空或非整形");
        }
        return new BaseResponse<>(goodslist_service.delete_good(order_id,good_id));
    }
}
